/*
 * $Id: TaskReloader.java,v 1.3 2009/11/02 21:51:06 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.common.util;

import java.util.List;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.jms.core.JmsTemplate;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.dao.ITaskDAO;
import com.ge.healthcare.autosc.common.database.pojo.ActTask;
import com.ge.healthcare.autosc.common.dbLogger.StepLogger;

/**
 * @author 212042764
 * 
 */
public class TaskReloader implements InitializingBean {
	private ITaskDAO taskDAO = null;
	private JmsTemplate jmsTemplate = null;
	private ApplicationInfo applicationInfo = null;
	private StepLogger stepLogger = null;

	/**
	 * Spring likes me
	 */
	public TaskReloader() {
		super();
	}

	/**
	 * Spring Bean cycle method
	 */
	public void afterPropertiesSet() throws Exception {
		this.reloadFromDB();
	}

	/**
	 * Reloads currently queued tasks from DB
	 */
	public void reloadFromDB() {
		String methodName = "reloadFromDB";
		try {
			String reloadingCodeName = ApplicationInfo.getInstance()
					.getProperties().getProperty(
							PropertyConstants.RELOADABLE_CODE_NAMES);
			ASCLogger.info(this.getClass(), methodName,
					"Reloading tasks with code names:" + reloadingCodeName);

			if (ASCUtil.isNullOrEmpty(reloadingCodeName)) {
				return;
			}
			final String codeNames[] = reloadingCodeName.split(",");
			for (String codeName : codeNames) {
				this.reloadFromDB(codeName);
			}
		} catch (RuntimeException runtimeException) {
			ASCLogger.error(this.getClass(), methodName,
					"Failed reloading tasks from DB, Exception:"
							+ runtimeException.getMessage(), runtimeException);
			throw runtimeException;
		}
	}

	/**
	 * Reloads queued tasks using codeName
	 * 
	 * @param codeName
	 */
	private void reloadFromDB(String codeName) {
		String methodName = "reloadFromDB";
		ASCLogger.info(this.getClass(), methodName,
				"Reloading tasks from Database with code name:" + codeName);
		List<ActTask> tasksToQueue = taskDAO.getTasksByCodeNameAndStepTypeId(
				codeName, StepLogger.getStepTypeId());
		if (!ASCUtil.isNullOrEmptyList(tasksToQueue)) {
			for (ActTask task : tasksToQueue) {
				ASCLogger.info(this.getClass(), methodName,
						"Reloading task... " + task);
				jmsTemplate.convertAndSend(task);
			}
		}
		ASCLogger.info(this.getClass(), methodName,
				"Finished Reloading tasks from Database with code name:"
						+ codeName);
	}

	/**
	 * @return the applicationInfo
	 */
	public ApplicationInfo getApplicationInfo() {
		return applicationInfo;
	}

	/**
	 * @param applicationInfo
	 *            the applicationInfo to set
	 */
	public void setApplicationInfo(ApplicationInfo applicationInfo) {
		this.applicationInfo = applicationInfo;
	}

	/**
	 * @return the stepLogger
	 */
	public StepLogger getStepLogger() {
		return stepLogger;
	}

	/**
	 * @param stepLogger
	 *            the stepLogger to set
	 */
	public void setStepLogger(StepLogger stepLogger) {
		this.stepLogger = stepLogger;
	}

	/**
	 * @param taskDAO
	 *            the taskDAO to set
	 */
	public void setTaskDAO(ITaskDAO taskDAO) {
		this.taskDAO = taskDAO;
	}

	/**
	 * @param jmsTemplate
	 *            the jmsTemplate to set
	 */
	public void setJmsTemplate(JmsTemplate jmsTemplate) {
		this.jmsTemplate = jmsTemplate;
	}
}
